<template>
	<view style="background-color: #fff;height: 100vh;">
		<view style="height: 900rpx;width: 100%;background-color: #fff;">
			<view style="height: 500rpx;width: 100%;">
				<view style="height: 170rpx;">
					<view style="height: 60rpx;width: 100%;font-size: 32rpx;display: flex;">
						<view style="width: 100%;text-align: center;line-height: 32rpx;margin-top: 100rpx;">
							<view style="font-size: 32rpx;height: 36rpx;"></view>
							<view style="height: 0.5rpx;width: 100%;margin-top: 24rpx;">
							</view>
						</view>
					</view>
				</view>
				<view style="height: 160rpx;">
					<view style="height: auto;display: flex;" class="padding-lr-lg">
						<!-- <view class="avatar edit" style="width: 20%;" @click="editUserInfo" v-if="isUserEdit == 1">
							<image class="cu-avatar round xl" :src="userInfo.headImg"></image>
						</view>
						<view class="avatar" style="width: 20%;" v-else>
							<image class="cu-avatar round xl" :src="userInfo.headImg"></image>
						</view> -->
						<view class="avatar edit" style="width: 20%;" @click="editUserInfo">
							<image class="cu-avatar round xl" :src="userInfo.headImg"></image>
						</view>
						<view style="width: 80%;" class="padding-sm">
							<view style="height: 36%;font-size: 36rpx;font-weight: 600;overflow: hidden;" class="text-black">
								<view>
									{{userInfo.realName||userInfo.userName||''}}
									<!-- <text class="cuIcon-medal text-green  margin-left-xs"></text> -->
								</view>
							</view>
							<view style="height: 30%;font-size: 28rpx;overflow: hidden;" class="text-black">
								<view>
									{{userInfo.universityName||''}}
									<!-- <text class="cuIcon-medal text-green  margin-left-xs"></text> -->
								</view>
							</view>
							<view style="height: 33%;font-size: 36rpx;font-weight: 600;" class="text-black">
								<view class='cu-tag round text-white' style="background-color: #F2828A;">
									<text class="margin-left-xs"> 唯一ID：</text>
									<text>{{userInfo.uniCode||''}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view style="height: 140rpx;display: flex;align-items: center;justify-content: center;">
					<view style="height: 140rpx;width: 25%;">
						<view
							style="height: 50%;display: flex;align-items: center;justify-content: center;font-size: 32rpx;font-weight: 1000;">
							{{userInfo.totalCredit||0}}
						</view>
						<view style="height: 50%;text-align: center;font-size: 28rpx;font-weight: 600;">总爱心值</view>
					</view>
					<view style="height: 80rpx;width:4rpx;background-color: #D8D8D8;"></view>
					<view style="height: 140rpx;width: 25%;">
						<view
							style="height: 50%;display: flex;align-items: center;justify-content: center;font-size: 32rpx;font-weight: 1000;">
							{{userInfo.donateCredit||0}}
						</view>
						<view style="height: 50%;text-align: center;font-size: 28rpx;font-weight: 600;">已助力值</view>
					</view>
					<view style="height: 80rpx;width:4rpx;background-color: #D8D8D8;"></view>
					<view style="height: 140rpx;width: 25%;">
						<view
							style="height: 50%;display: flex;align-items: center;justify-content: center;font-size: 32rpx;font-weight: 1000;">
							{{userInfo.availCredit > 0 ? userInfo.availCredit : 0}}
						</view>
						<view style="height: 50%;text-align: center;font-size: 28rpx;font-weight: 600;">可助力值</view>
					</view>
					<view style="height: 80rpx;width:4rpx;background-color: #D8D8D8;"></view>
					<view style="height: 140rpx;width: 25%;">
						<view
							style="height: 50%;display: flex;align-items: center;justify-content: center;font-size: 32rpx;font-weight: 1000;">
							{{userInfo.currentCredit > 0 ? userInfo.currentCredit : 0}}
						</view>
						<view style="height: 50%;text-align: center;font-size: 28rpx;font-weight: 600;">荣誉值</view>
					</view>
				</view>
			</view>
			<view style="height: 160rpx;display: flex;" @click="donationPoints()">
				<image :src="url" mode="aspectFit" style="height: 160rpx;margin: 0 auto;" ></image>
				<!-- <view class="text-white" style="height:260rpx;position: absolute;padding: 60rpx 60rpx;">
					<view  style="font-size: 36rpx;font-weight: 500;margin: 20rpx;">点击捐赠</view>
					<view style="font-size: 24;font-weight: 400;margin: 20rpx;">获得的积分记得要捐给本校～</view>
				</view> -->
			</view>
		
			<view style="height: 80rpx;width: 100%;padding: 60rpx;font-size: 28rpx;font-weight: 400;color: rgba(0, 0, 0, 0.5)">
				<text class="cuIcon-info text-grey  margin-right-xs text-lg"></text>
				助力积分不会影响同学的总体统计情况，请将积分捐赠给您所在的爱心团即可。
			</view>
			<view style="height: 80rpx;width: 100%;padding: 60rpx;font-size: 28rpx;font-weight: 400;color: rgba(0, 0, 0, 0.5)">
				<text class="cuIcon-info text-grey  margin-right-xs text-lg"></text>
				荣誉值等于总爱心值，同学可以用于兑换荣誉证书及其他福利，助力爱心值不会影响荣誉值
			</view>
			
			<view class="operation-list">
				<navigator url="../../subpages/order/order-list" hover-class="navigator-hover">订单管理</navigator>
				<navigator url="../../subpages/integral/integral" hover-class="navigator-hover">积分明细</navigator>
				<navigator url="../../subpages/donation/integral" hover-class="navigator-hover">助力明细</navigator>
				<navigator url="../../subpages/address/address" hover-class="navigator-hover">地址管理</navigator>
			</view>
			
			<view class="activity-list" v-if="activityList.length > 0">
				<view class="item" v-for="(item, index) in activityList" :key="index" style="width: auto;">
					<button @click="jumpTo(item)">{{ item.activityName }}</button>
				</view>
			</view>
			<view class="adContainer">
			  <ad unit-id="adunit-9a2b0e23252035e2" ad-intervals="30"></ad>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserProfile
	} from '@/utils/getUserProfile.js'

	import {
		getUserInfo,
		getGradelist
	} from '@/api/user.js'
	
	import {
		getActivityList
	} from '@/api/activity.js'

	let videoAd = null
	export default {
		data() {
			return {
				url: require('../../static/user.png'),
				userInfo: '',
				activityList: [],
				isUserEdit: 0 // 是否修改用户信息
			}
		},
		onLoad() {
		},
		onReady() {

		},
		onTabItemTap(item) {
			let token = uni.getStorageSync("token");
			if (!token) {
				this.getUserProfile();
			} else {
				this.getUserInfo();
			}
		},
		onShow() {
			let token = uni.getStorageSync("token");
			if (token) {
				this.getUserInfo()
			}
		},
		methods: {
			// 链接地址跳转
			jumpTo(record) {
				if (record.url) {
					var regexp = /((http|https):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u4e00-\u9fa5\-\.\/?\@\%\!\&=\+\~\:\#\;\,]*)?)/ig;
					var url = record.url.match(regexp)
					// 匹配外部链接地址
					if (url && typeof(url)!="undefined" && url!=0) {
						uni.navigateTo({
							url: '../outlink?url=' + encodeURIComponent(record.url)
						})
						// window.open(encodeURI(urls));
					} else {
						uni.navigateTo({
							url: record.url
						})
					}
				} else {
					uni.navigateTo({
						url: '../activity/detail?id=' + record.activityId
					})
				}
			},
			/**授权登录*/
			async getUserProfile() {
				let data = await getUserProfile();
				if (data.code != 200) {
					uni.showToast({
						title: '登录失败！',
						duration: 1000,
						icon: 'none',
					});
				} else {
					uni.showToast({
						title: '登录成功！',
						duration: 1000,
						icon: 'none',
					});
					await this.getUserInfo();
				}
			},
			/**获取用户信息*/
			async getUserInfo() {
				let token = uni.getStorageSync("token");
				const {
					data
				} = await getUserInfo(token);
				this.userInfo = data;
				await this.getActivityList()
				await this.getConfig()
			},
			// 获取活动
			async getActivityList() {
				const query = {
					page: 1,
					pagesize: 6,
					categoryId: 2
				}
				getActivityList(query).then(res => {
					if (res.code === 200) {
						this.activityList = res.data.lists
					}
				})
			},
			// 用户配置信息
			async getConfig() {
				getGradelist().then(data => {
					console.log(data);
					if (data.code === 200) {
						this.isUserEdit = data.data.isOpenEdit
						// this.isUserEdit = 1
					}
				})
			},
			/**捐赠积分*/
			donationPoints() {
				if (this.userInfo.realName) {
					uni.navigateTo({
						url: '../../subpages/donation/donation1'
					})
				} else {
					uni.showToast({
						title: '请先完善个人信息！',
						duration: 1000,
						icon: 'none',
					});
					setTimeout(() => {
						uni.navigateTo({
							url: '../../subpages/user/edit'
						})
					}, 1000)
				}
			},
			/**修改用户信息**/ 
			editUserInfo() {
				uni.navigateTo({
					url: '../../subpages/user/edit'
				})
			}
		}
	}
</script>

<style lang="less" scoped>
@import './index.less';
.adContainer{width: 100%;}
.operation-list{
	display: flex;
	justify-content: space-between;
	margin-top: 60rpx;
	border-top: 1px solid #ddd;
	border-bottom: 1px solid #ddd;
	height: 120rpx;
	line-height: 120rpx;
	navigator{
		flex-grow: 1;
		text-align: center;
		font-size: 34rpx;
		position: relative;
		&::after{
			content: "|";
			position: absolute;
			right: 0;
		}
		&:last-child::after{
			content: ""
		}
	}
}
</style>
